package com.yq.reggie.controller;

import com.aliyuncs.http.HttpResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yq.reggie.common.BaseContext;
import com.yq.reggie.pojo.AddressBook;
import com.yq.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 添加收获地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result add(@RequestBody AddressBook addressBook){
        log.info("添加收获地址...");
        //完善addressBook
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        //向address_book表添加数据
        // sql:insert into address_book( id, user_id, consignee, sex, phone, detail, label, create_time, update_time, create_user, update_user ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
        addressBookService.save(addressBook);
        return Result.success("地址添加成功");
    }

    /***
     * 展示收获地址
     * @return
     */
    @GetMapping("/list")
    public Result selectList(HttpServletResponse response)  {
        //创建条件构造器
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userId);
        //查询address_book表
        //sql: select * from address_book where user_id=?
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook){
        //修改当前用户对应address_book表的is_default=0
        //sql: update address_book set is_default=? where user_id=?;
        LambdaUpdateWrapper<AddressBook> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.set(AddressBook::getIsDefault,0);
        Long userId = BaseContext.getCurrentId();
        queryWrapper.eq(AddressBook::getUserId,userId);
        addressBookService.update(queryWrapper);
        //设置当前id对应address_book表的属性is_default=1
        //sql:update address_book set is_default=?,update_time=?,update_user=? where id=?;
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return Result.success("设置默认成功");
    }

    /**
     * 展示默认地址
     * @return
     */
    @GetMapping("/default")
    public Result displayDefault(){
        //根据user_id和is_default查询address_book表
        //sql: select * from address_book where user_id=? and is_default=?
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userId);
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        return Result.success(addressBook);
    }
}
